<?php
namespace Kxdrmobile\Model;
use Think\Model;
class GuessModel{
	public $uid;
	public $sid;
	
	// loadSoccerDiffByGuess
	// 处理足球比赛的猜分差距。计算分差（猜中、没猜中并记录在user_guess表中）
	// 10分钟计算一次？1分钟计算一次？
	public function saveSoccerDiff(){
		$GUESS = D("SoccerguessView"); 
		$guess = $GUESS->where("`guess`.`user_id`=".$this->uid." AND `event`.`sid`=".$this->sid." AND `guess_value_difference` IS NULL AND `game`.`status`=3")
					   ->getField("id,game_status,player_title,eid,player_id,epid,think_type,think_type_sign,think_type_way,guess_type,guess_value,target_data,target_data_sign,game_id,game_name,home_team,guest_team,private_home_team_id,private_guest_team_id,private_home_team_id,home_team_score,home_team_redcard,home_team_yellowcard,home_team_halfscore,home_shoot,home_target_shoot,home_woodwork_shoot,home_straight_plug_ball,home_offside,home_steals,home_free_kick,home_foul,home_corner,home_foul_ball,home_over_25_yard_passes,home_pass_success_rate,home_pass_center_success_rate,home_steal_success_rate,home_header_success_rate,home_ball_control_rate,guest_team_halfscore,guest_team_yellowcard,guest_team_redcard,guest_team_score,guest_shoot,guest_target_shoot,guest_woodwork_shoot,guest_straight_plug_ball,guest_offside,guest_steals,guest_free_kick,guest_foul,guest_corner,guest_foul_ball,guest_over_25_yard_passes,guest_pass_success_rate,guest_pass_center_success_rate,guest_steal_success_rate,guest_header_success_rate,guest_ball_control_rate,game_status,score_status");
		// echo $GUESS->_sql();
		// print_r($guess);
		foreach($guess as $gu){
			// 判断预测的是主队还是客队
			if($gu["player_id"] === $gu["private_home_team_id"]){
				$clm = "home_";
			}elseif($gu["player_id"] === $gu["private_guest_team_id"]){
				$clm = "guest_";
			}else{
				$clm="none";
			}
						
			if($gu["think_type_sign"]=="win"){ // 处理猜胜负，需要比较主客队数据。这里没有考虑平局的情况
				$win = $gu["home_team_score"] - $gu["guest_team_score"];
				if($clm=="guest_"){
					$win=(0-$win);//客队取胜的情况
				}
				
				//判断结果是否与猜测符合
				if($win>0 && ($gu["guess_type"]=="yes" || $gu["guess_type"]=="only_y")){ 
					// 猜胜利成功，猜中的奖品
					unset($t);
					$t["guess_id"]=$gu["id"];
					$t["guess_value_difference"]=0;
					$gift[]=$t;
				}
				
				if($win<=0 && ($gu["guess_type"]=="yes" || $gu["guess_type"]=="only_y")){ 
					// 猜胜利失败，没猜中的奖品
					unset($t);
					$t["guess_id"]=$gu["id"];
					$t["guess_value_difference"]=-1;
					$gift[]=$t;
				}
				
				if($win<=0 && ($gu["guess_type"]=="no" || $gu["guess_type"]=="only_n")){ 
					// 猜失败成功,猜中的奖品
					unset($t);
					$t["guess_id"]=$gu["id"];
					$t["guess_value_difference"]=0;
					$gift[]=$t;
				}
				
				if($win>0 && ($gu["guess_type"]=="no" || $gu["guess_type"]=="only_n")){ 
					// 猜失败失败,没猜中的奖品
					unset($t);
					$t["guess_id"]=$gu["id"];
					$t["guess_value_difference"]=-1;
					$gift[]=$t;
				}
			}elseif($gu["think_type_sign"]=="draw"){ //处理猜平局
				$draw = $gu["home_team_score"] == $gu["guest_team_score"];
				if($draw && ($gu["guess_type"]=="yes" || $gu["guess_type"]=="only_y")){
					// 猜平局成功，猜中的奖品
					unset($t);
					$t["guess_id"]=$gu["id"];
					$t["guess_value_difference"]=0;
					$gift[]=$t;
				}
				if($draw && ($gu["guess_type"]=="no" || $gu["guess_type"]=="only_n")){
					// 猜平局失败，没猜中的奖品
					unset($t);
					$t["guess_id"]=$gu["id"];
					$t["guess_value_difference"]=-1;
					$gift[]=$t;
				}
				if(!$draw && ($gu["guess_type"]=="no" || $gu["guess_type"]=="only_n")){
					// 猜不平局成功，猜中的奖品
					unset($t);
					$t["guess_id"]=$gu["id"];
					$t["guess_value_difference"]=0;
					$gift[]=$t;
				}
				if(!$draw && ($gu["guess_type"]=="yes" || $gu["guess_type"]=="only_y")){
					// 猜不平局失败，没猜中的奖品
					unset($t);
					$t["guess_id"]=$gu["id"];
					$t["guess_value_difference"]=-1;
					$gift[]=$t;
				}
			}elseif($gu["think_type_way"]>0){// 处理胜负和平局以外的只猜是、只猜否、猜是和否，例子：进球是否能超过2个？
				$hit = false;
				switch($gu["target_data_sign"]){
					case 0 :  // 0：正向预测不含当前值
						$hit = $gu[$clm.$gu["think_type_sign"]]>$gu["target_data"];  //射门是否超过10次（不含）？
					break;
					case 1 :  // 1：正向预测含当前值
						$hit = $gu[$clm.$gu["think_type_sign"]]>=$gu["target_data"];  //射门是否超过10次（含）？
					break;
					case 2 :  // 2：负向预测不含当前值
						$hit = $gu[$clm.$gu["think_type_sign"]]<$gu["target_data"];  //射门是否不到10次（不含）？
					break;
					case 3:   // 3：负向预测含当前值
						$hit = $gu[$clm.$gu["think_type_sign"]]<=$gu["target_data"];  //射门是否不到10次（含）？
					break;
					
				}
				if($hit && ($gu["guess_type"]=="yes" || $gu["guess_type"]=="only_y"))  //猜是猜中
				{
					unset($t);
					$t["guess_id"]=$gu["id"];
					$t["guess_value_difference"]=0;
					$gift[]=$t;
				}
				if(!$hit && ($gu["guess_type"]=="yes" || $gu["guess_type"]=="only_y"))  //猜是失败
				{
					unset($t);
					$t["guess_id"]=$gu["id"];
					$t["guess_value_difference"]=-1;
					$gift[]=$t;
				}
				if($hit && ($gu["guess_type"]=="no" || $gu["guess_type"]=="only_n"))  //猜否猜中
				{
					unset($t);
					$t["guess_id"]=$gu["id"];
					$t["guess_value_difference"]=0;
					$gift[]=$t;
				}
				if(!$hit && ($gu["guess_type"]=="no" || $gu["guess_type"]=="only_n"))  //猜否失败
				{
					unset($t);
					$t["guess_id"]=$gu["id"];
					$t["guess_value_difference"]=-1;
					$gift[]=$t;
				}
					
			}else{
				// 处理猜数据
				unset($diff);
				$diff = abs($gu[$clm.$gu["think_type_sign"]] - $gu["guess_value"])  ; //计算出猜分与实际分差
				$t["guess_id"]=$gu["id"];
				$t["guess_value_difference"]=$diff;
				$gift[]=$t;
			}
			
		}
		
		if($gift)
		{
			//读取称号加分设置
			$TOOL = D("Kxdr/ToolView");
			$cfg = json_decode($TOOL->where("`status`.`sid`=".$this->sid." AND `templet_code`='kxdr_honor'")->getField("cfg"),true);
				
			//批量更新所有猜分差距
			$sql = "UPDATE sm_kxdr_user_guess SET guess_value_difference = CASE id ";
			$nengli=0;
			$win_guess=0;
			foreach($gift as $g){
				$ids[]=$g["guess_id"];
				$sql.=" WHEN ".$g["guess_id"]." THEN ".$g["guess_value_difference"];
				
				//为用户增加预测能力值
				$nengli += $cfg["honor_title"]["action"]; // 参与活动加分
				if($g["guess_value_difference"]==0){
					$win_guess++;
					$nengli += $cfg["honor_title"]["exact"];// 成功预测加分
				}
				if($g["guess_value_difference"]!=0){
					$nengli += $cfg["honor_title"]["fault"];// 预测失败加分
				}
				
			}
			$sql .=" END WHERE id IN (".implode(",",$ids).")";
			$gift["total"] = $GUESS->execute($sql);
			
			if($nengli>0 || $win_guess>0){
				if($nengli>0){
					$usql[]="`nengli`=`nengli`+".$nengli;
				}
				if($win_guess>0){
					$usql[]="`win_guesses`=`win_guesses`+".$win_guess;
				}
				$usr_sql = "UPDATE `sm_kxdr_user` SET ".implode(",",$usql)." WHERE `id`=".$this->uid;
				$GUESS->execute($usr_sql);
				
				$this->nengli_inc = $nengli;
			}
		}
		return $gift;
	}
}